//
//  PickFoodTableView.m
//  ZooNgonDoiOS
//
//  Created by Trung sound on 8/9/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "PickFoodTableView.h"
#import "PickFoodItemTableViewCell.h"
#import "ZooFoodBean.h"

@implementation PickFoodTableView 
@synthesize parentView,arFoodChecked,arFoodNoneCheck;

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
//        _arFoodName = [[NSMutableArray alloc]initWithObjects:@"Pho cuon", @"Pho cuon1",@"Pho cuon2",@"Pho cuon3",@"Pho cuon4",@"Bun bo5", @"Pho cuon6", @"Pho cuon7",@"Pho cuon8",@"Pho cuon9",@"Pho cuon10", @"Pho cuon11", @"Pho cuon12",@"Pho cuon13",@"Pho cuon14",@"Pho cuon15", nil];
//        _arFoodCost = [[NSMutableArray alloc]initWithObjects:@"45", @"50",@"51",@"52",@"53",@"54",@"45", @"50",@"51",@"52",@"53",@"54",@"45", @"50",@"51",@"52", nil];
        self.delegate = self;
        self.dataSource = self;
        
        arFoodChecked = [[NSMutableArray alloc]init];
        arFoodNoneCheck = [[NSMutableArray alloc]init];

    }
    return self;
}

- (void)initHeaderTable:(UITableViewCell *)pCell {
    _vHeader = [[UIView alloc]initWithFrame:CGRectMake(11, 8, 298, 39)];
    [_vHeader setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_SEARCH]]];
    
    _btnSearch = [[UIButton alloc]initWithFrame:CGRectMake(8, 8, 24, 24)];
    [_btnSearch setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_BTN_SEARCH]]];
    
    _tfSearch = [[UITextField alloc]initWithFrame:CGRectMake(45, 8, 240, 20)];
    [_tfSearch setPlaceholder:STRING_SEARCH_HINT];
    _tfSearch.textAlignment = UITextAlignmentLeft;
    
    [_vHeader addSubview:_btnSearch];
    [_vHeader addSubview:_tfSearch];
    
    [pCell.contentView addSubview:_vHeader];
}

- (void)initFooterTable:(UITableViewCell *)pCell {
    UIButton *btnAddFood = [[UIButton alloc]initWithFrame:CGRectMake(11, 10, 297, 35)];
    [btnAddFood setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_ADDFOOD_BACKGROUND]]];
    [btnAddFood addTarget:self action:@selector(btnAddFoodClicked) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *vAddFoodImg = [[UIImageView alloc]initWithFrame:CGRectMake(75, 4, 30, 27)];
    [vAddFoodImg setImage:[UIImage imageNamed:IMG_BTN_ADDFOOD]];
    
    UILabel *lbAddFood = [[UILabel alloc]initWithFrame:CGRectMake(110, 4, 150, 27)];
    [lbAddFood setBackgroundColor:[UIColor clearColor]];
    [lbAddFood setText:STRING_ADD_NEW_FOOD];
    [lbAddFood setTextColor:[UIColor whiteColor]];
    [lbAddFood setFont:[UIFont systemFontOfSize:12]];
    
    [btnAddFood addSubview:vAddFoodImg];
    [btnAddFood addSubview:lbAddFood];
    
    UILabel *lbSupport = [[UILabel alloc]initWithFrame:CGRectMake(11, 50, 295, 30)];
    [lbSupport setText:@"Hỗ trợ bởi ZooStudio"];
    [lbSupport setTextAlignment:UITextAlignmentCenter];
    [lbSupport setFont:[UIFont systemFontOfSize:12]];
    [lbSupport setTextColor:[UIColor grayColor]];
    [lbSupport setBackgroundColor:[UIColor clearColor]];
    
    [pCell.contentView addSubview:btnAddFood];
    [pCell.contentView addSubview:lbSupport];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    switch (section) {
        case 0:
            return 1;
        case 1:
            return [arFoodNoneCheck count];
        case 2:
            return 1;
        default:
            break;
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier0 = @"cellIdentifier0";
    NSString *cellIdentifier1 = @"cellIdentifier1";
    NSString *cellIdentifier2 = @"cellIdentifier2";
    
        // backup array
    _arFoodCheckedBK = arFoodChecked;
    _arFoodNoneCheckBK = arFoodNoneCheck;
    
    UITableViewCell *_cell = nil;
    PickFoodItemTableViewCell *_cellContent;
    
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    switch (indexPath.section) {
        case 0:
            _cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier0];    
            if (_cell == nil) {
                _cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier0];
                [_cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            }
            [self initHeaderTable:_cell];
            break;
        case 1:

            _cellContent = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];    
            if (_cellContent == nil) {
                _cellContent = [[PickFoodItemTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier1];
            }
            ZooFoodBean *foodBean = [[ZooFoodBean alloc]init];
            foodBean = [arFoodNoneCheck objectAtIndex:indexPath.row];
            
            _cellContent.lbFoodName.text = foodBean.foodName;
            _cellContent.lbFoodCost.text = foodBean.foodCost;

            [_cellContent.btnView0 addTarget:self action:@selector(btnView0Clicked:) forControlEvents:UIControlEventTouchUpInside];
            
            _cell = _cellContent;
            
            [_cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            break;
            
        case 2:
            _cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
            if (_cell == nil) {
                _cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier2];
                [_cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            }
            [self initFooterTable:_cell];
            break;
            
        default:
            break;
    }
    
    return _cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
        return 100;
    }
    return 75;
}

- (void)btnView0Clicked:(id)sender {
    PickFoodItemTableViewCell *cell = (PickFoodItemTableViewCell *)[sender superview];
//    cell.btnView0.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:IMG_PICKFOODROW_SELECTED]];

    [arFoodChecked addObject:[arFoodNoneCheck objectAtIndex:[self indexPathForCell:cell].row]];
    [parentView setForArrayFoodAte:arFoodChecked];
    NSLog(@"%d",[self indexPathForCell:cell].row);
    NSLog(@"%@",cell.lbFoodName.text);    
    
    [self beginUpdates];
    [arFoodNoneCheck removeObjectAtIndex:[self indexPathForCell:cell].row];
    [self deleteRowsAtIndexPaths:[NSArray arrayWithObject:[self indexPathForCell:cell]] withRowAnimation:UITableViewRowAnimationRight];
    [self endUpdates];
}

- (void)btnAddFoodClicked {
    [parentView pushAddFoodViewController];
}

@end
